package com.shcd.mapperImpl;

import com.shcd.mapper.DataLifeCycleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class DataLifeCycleMapperImpl implements DataLifeCycleMapper {

    private final DataLifeCycleMapper dataLifeCycleMapper;

    @Autowired
    public DataLifeCycleMapperImpl(DataLifeCycleMapper dataLifeCycleMapper) {
        this.dataLifeCycleMapper = dataLifeCycleMapper;
    }

    @Override
    public List<Map<String, String>> SelectCids() {
        return dataLifeCycleMapper.SelectCids();
    }

    @Override
    public List<Map<String, String>> SelectDescribe(Map<String, String> m) {
        return dataLifeCycleMapper.SelectDescribe(m);
    }

    @Override
    public void InsertData(List<Map<String, String>> m) {
        dataLifeCycleMapper.InsertData(m);
    }

    @Override
    public List<Map> SelectSum_LOG_EXCHANGE(Map<String, Object> m) {
        return dataLifeCycleMapper.SelectSum_LOG_EXCHANGE(m);
    }

    @Override
    public List<Map> SelectSize_Sum_LOG_EXCHANGE(Map<String, Object> m) {
        return dataLifeCycleMapper.SelectSize_Sum_LOG_EXCHANGE(m);
    }

    @Override
    public List<Map> SelectLOG_EXCHANGEbyInserttime(Map<String, Object> m) {
        return dataLifeCycleMapper.SelectLOG_EXCHANGEbyInserttime(m);
    }

    @Override
    public List<Map> SelectSize_LOG_EXCHANGEbyInserttime(Map<String, Object> m) {
        return dataLifeCycleMapper.SelectSize_LOG_EXCHANGEbyInserttime(m);
    }

    @Override
    public List<Map> SelectDescribe_LOG_EXCHANGE(Map<String, Object> m) {
        return dataLifeCycleMapper.SelectDescribe_LOG_EXCHANGE(m);
    }

    @Override
    public List<Map> SelectSize_Describe_LOG_EXCHANGE(Map<String, Object> m) {
        return dataLifeCycleMapper.SelectSize_Describe_LOG_EXCHANGE(m);
    }

    @Override
    public List<Map> Select_queryLogList(Map m) throws ParseException {
        //根据时间比对，当前时间大于最新运行时间x小时状态是红是绿是空
        List<Map> LogList =  dataLifeCycleMapper.Select_queryLogList(m);
        List<Map> logListNew = new ArrayList<>();
        String dateStates = m.get("dateStates").toString();//数据状态
        long xzDate = 360; //限制时间（分）
        for(Map<String,String> filed  : LogList){
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            Map<String,String> listMap = new HashMap<>(); //存放判断后的Map；
            Date sysdate =  simpleDateFormat.parse(simpleDateFormat.format(new Date())); //当前时间
            String gjStates  = String.valueOf(filed.get("GJ_STATES"));
            String providerUnit = String.valueOf(filed.get("PROVIDERUNIT")); //编目单位编号
            String catalogName = filed.get("NAME");
            String kettleRunTime = String.valueOf(filed.get("KETTLE_RUN_TIME"));
            String zjRunTime = String.valueOf(filed.get("ZJ_RUN_TIME"));
            String qxRunTime = String.valueOf(filed.get("QX_RUN_TIME"));
            //如果在ptgl.catalog_interface表中未找到对应8位码即为0，反之为1
            if(gjStates.equals("-")){
                listMap.put("GJ_STATES","0");
            }else{
                listMap.put("GJ_STATES","1");
            }
            listMap.put("CATALOG_NAME",catalogName);
            listMap.put("KETTLE_RUN_TIME",kettleRunTime);
            listMap.put("ZJ_RUN_TIME",zjRunTime);
            listMap.put("QX_RUN_TIME",qxRunTime);
            listMap.put("PROVIDERUNIT",providerUnit);
            Date kettleTime = simpleDateFormat.parse(kettleRunTime);
            Date zjTime = simpleDateFormat.parse(zjRunTime);
            Date qxTime = simpleDateFormat.parse(qxRunTime);
            CalculateDate(simpleDateFormat,listMap, xzDate, sysdate,kettleTime,"KETTLE_STATES");
            CalculateDate(simpleDateFormat,listMap, xzDate, sysdate,zjTime,"ZJ_STATES");
            CalculateDate(simpleDateFormat,listMap, xzDate, sysdate,qxTime,"QX_STATES");
            String zjStates = listMap.get("ZJ_STATES");
            String qxStates = listMap.get("QX_STATES");
            String kettleStates = listMap.get("KETTLE_STATES");
            //熔断 如果归集状态是 0 则其他3个状态都是0
            if(gjStates.equals("-")){
                listMap.put("KETTLE_STATES","0");
                listMap.put("ZJ_STATES","0");
                listMap.put("QX_STATES","0");
            }

            if(dateStates.equals("-1")) { //All
                logListNew.add(listMap);
            }
            if(dateStates.equals("0")){ //全部状态都是0
                if(zjStates.equals("0")&&qxStates.equals("0")&&kettleStates.equals("0")){
                    logListNew.add(listMap);
                }
            }
            if(dateStates.equals("1")){//全部状态都是1
                if(zjStates.equals("1")&&qxStates.equals("1")&&kettleStates.equals("1")){
                    logListNew.add(listMap);
                }
            }
            if(dateStates.equals("2")){//一个异常一个状态是2
                if(zjStates.equals("2")||qxStates.equals("2")||kettleStates.equals("2")){
                    logListNew.add(listMap);
                }
            }
        }
        return logListNew;
    }

    @Override
    public void Insert_queryLogList(List<Map> m) {
        dataLifeCycleMapper.Insert_queryLogList(m);
    }

    @Override
    public List<Map> queryLogList(Map<String, Object>  m) {
        return dataLifeCycleMapper.queryLogList(m);
    }

    @Override
    public void Delete_queryLogList() {
        dataLifeCycleMapper.Delete_queryLogList();
    }

    private void CalculateDate(SimpleDateFormat simpleDateFormat, Map<String, String> listMap,
                               long xzDate, Date sysdate , Date startTime, String filedName) throws ParseException {
        long nd = 1000 * 24 * 60 * 60; //天
        long nh = 1000 * 60 * 60;//小时
        long nm = 1000 * 60;//分
        Date invalidTime = simpleDateFormat.parse("1970-01-01 00:00:00.0"); //无效日期
        if(startTime.getTime()!=invalidTime.getTime()){
            long diff = sysdate.getTime() - startTime.getTime();
            // 计算差多少天
            long day = diff / nd;
            // 计算差多少小时
            long hour = diff % nd / nh;
            // 计算差多少分钟
            long min = diff % nd % nh / nm;
            // 计算差多少秒//输出结果
            if(min>xzDate||day>=1||hour>4){ //和当前时间比较超过限制时间状态即为红色，反之为绿色
                listMap.put(filedName,"2");
            }else{
                listMap.put(filedName,"1");
            }
        }else{ //空值为0
            listMap.put(filedName,"0");
        }
    }
}
